<template>
    <div>
        <!-- 查询条件开始 -->
        <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
            <el-form-item label="药品名称" prop="medicinesName">
                <el-input
                        v-model="queryParams.medicinesName"
                        placeholder="请输入药品名称"
                        clearable
                        size="small"
                        style="width:180px"
                />
            </el-form-item>
            <el-form-item label="关键字" prop="keywords">
                <el-input
                        v-model="queryParams.keywords"
                        placeholder="请输入关键字"
                        clearable
                        size="small"
                        style="width:180px"
                />
            </el-form-item>
            <el-form-item label="药品类型" prop="medicinesType">
                <el-select
                        v-model="queryParams.medicinesType"
                        placeholder="药品类型"
                        clearable
                        size="small"
                        style="width:180px"
                >
                    <el-option
                            v-for="dict in medicinesTypeOptions"
                            :key="dict.dictValue"
                            :label="dict.dictLabel"
                            :value="dict.dictValue"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="生产厂家" prop="producerId">
                <el-select
                        v-model="queryParams.producerId"
                        placeholder="生产厂家"
                        clearable
                        size="small"
                        style="width:180px"
                >
                    <el-option
                            v-for="dict in producerOptions"
                            :key="dict.producerId"
                            :label="dict.producerName"
                            :value="dict.producerId"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="处方类型" prop="prescriptionType">
                <el-select
                        v-model="queryParams.prescriptionType"
                        placeholder="处方类型"
                        clearable
                        size="small"
                        style="width:180px"
                >
                    <el-option
                            v-for="dict in prescriptionTypeOptions"
                            :key="dict.dictValue"
                            :label="dict.dictLabel"
                            :value="dict.dictValue"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select
                        v-model="queryParams.status"
                        placeholder="可用状态"
                        clearable
                        size="small"
                        style="width:180px"
                >
                    <el-option
                            v-for="dict in statusOptions"
                            :key="dict.dictValue"
                            :label="dict.dictLabel"
                            :value="dict.dictValue"
                    />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" >搜索</el-button>
                <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery" >重置</el-button>
            </el-form-item>
        </el-form>
        <!-- 查询条件结束 -->
        <!-- 表格工具按钮开始 -->
        <el-row :gutter="10" style="margin-bottom: 8px;">
            <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDeleteTop">删除</el-button>
            </el-col>
        </el-row>
        <!-- 表格工具按钮结束 -->
        <!-- 数据表格开始 -->
        <el-table border :data="medicinesTableList" @selection-change="handleSelectionChnage">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column type="expand">
                <template slot-scope="props">
                    <el-form label-position="left" inline class="demo-table-expand">
                        <el-form-item label="单位">
                            <span>{{ props.row.unit }}</span>
                        </el-form-item>
                        <el-form-item label="换算量">
                            <span>{{ props.row.conversion }}</span>
                        </el-form-item>
                        <el-form-item label="库存量">
                            <span>{{ props.row.medicinesStockNum }}</span>
                        </el-form-item>
                        <el-form-item label="预警值">
                            <span>{{ props.row.medicinesStockDangerNum }}</span>
                        </el-form-item>
                    </el-form>
                </template>
            </el-table-column>
            <el-table-column label="药品ID" align="center" prop="medicinesId" />
            <el-table-column label="药品名称" align="center" prop="medicinesName" />
            <el-table-column label="药品编号" align="center" prop="medicinesNumber" />
            <!-- :formatter="producerFormatter"-->
            <el-table-column label="生产厂家" width="280px" align="center" prop="producerId" :formatter="producerFormatter"/>
            <!---->
            <el-table-column label="药品类型" align="center" prop="medicinesType" :formatter="medicinesTypeFormatter" />
            <!--  -->
            <el-table-column label="处方类型" align="center" prop="prescriptionType" :formatter="prescriptionTypeFormatter"/>
            <el-table-column label="关键字" align="center" prop="keywords" />
            <el-table-column label="处方价格" align="center" prop="prescriptionPrice" />
            <!---->
            <el-table-column label="状态" prop="status" align="center"  :formatter="statusFormatter"/>
            <el-table-column label="操作" width="280px" align="center">
                <template slot-scope="scope">
                    <el-button type="text" icon="el-icon-edit" size="mini" @click="handleUpdate(scope.row)">修改</el-button>
                    <el-button type="text" icon="el-icon-delete" size="mini" @click="handleDelete(scope.row)" >删除</el-button>
                    <el-button type="text" icon="el-icon-plus" size="mini" @click="handleUpdateStorage(scope.row)">调整库存</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 数据表格结束 -->
        <!-- 分页控件开始 -->
        <el-pagination
                v-show="total>0"
                :current-page="currentPage"
                :page-sizes="[5, 10, 20, 30]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
        />
        <!-- 分页控件结束 -->
        <!-- 添加修改弹出层开始 -->
        <el-dialog
                :title="title"
                :visible.sync="open"
                width="800px"
                center
                append-to-body
        >
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="药品名称" prop="medicinesName" >
                            <el-input v-model="form.medicinesName" style="width:240px" placeholder="请输入药品名称" clearable size="small" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="药品编码" prop="medicinesNumber">
                            <el-input v-model="form.medicinesNumber" style="width:240px" placeholder="请输入药品编码" clearable size="small" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="关键字" prop="keywords">
                            <el-input v-model="form.keywords" style="width:240px" placeholder="请输入关键字" clearable size="small" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="单位" prop="unit">
                            <el-input v-model="form.unit" style="width:240px" placeholder="请输入单位" clearable size="small" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="换算量" prop="conversion">
                            <el-input v-model="form.conversion" style="width:240px" placeholder="请输入换算量" clearable size="small" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="处方价格" prop="prescriptionPrice">
                            <el-input-number v-model="form.prescriptionPrice" style="width:240px" placeholder="请输入处方价格" size="small" :min="0"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="预警值" prop="medicinesStockDangerNum">
                            <el-input-number v-model="form.medicinesStockDangerNum" style="width:240px" placeholder="请输入预警值" size="small" :min="0"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="状态" prop="status">
                            <el-radio-group v-model="form.status" style="width:240px">
                                <el-radio
                                        v-for="dict in statusOptions"
                                        :key="dict.dictValue"
                                        :label="dict.dictValue"
                                        :value="dict.dictValue"
                                >{{ dict.dictLabel }}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="药品类型" prop="medicinesType">
                            <el-select
                                    v-model="form.medicinesType"
                                    style="width:240px"
                                    placeholder="药品类型"
                                    size="small"
                            >
                                <el-option
                                        v-for="dict in medicinesTypeOptions"
                                        :key="dict.dictValue"
                                        :label="dict.dictLabel"
                                        :value="dict.dictValue"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="处方类型" prop="prescriptionType">
                            <el-select
                                    v-model="form.prescriptionType"
                                    style="width:240px"
                                    placeholder="处方类型"
                                    size="small"
                            >
                                <el-option
                                        v-for="dict in prescriptionTypeOptions"
                                        :key="dict.dictValue"
                                        :label="dict.dictLabel"
                                        :value="dict.dictValue"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="生产厂家" prop="producerId">
                            <el-select
                                    v-model="form.producerId"
                                    style="width:240px"
                                    placeholder="生产厂家"
                                    size="small"
                            >
                                <el-option
                                        v-for="d in producerOptions"
                                        :key="d.producerId"
                                        :label="d.producerName"
                                        :value="d.producerId"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit('form')">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </span>
        </el-dialog>
        <!-- 添加修改弹出层结束 -->
    </div>
</template>

<script>
    export default {
        data(){
            return{
                // 查询参数
                queryParams: {
                    medicinesName: undefined,
                    medicinesType: undefined,
                    prescriptionType: undefined,
                    producerId: undefined,
                    status: undefined,
                    keywords: undefined,
                },
                medicinesIds:[],
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 字典表格数据
                medicinesTableList: [],
                // 状态数据字典
                statusOptions: [],
                //药品分类
                medicinesTypeOptions:[],
                //药品处方分类
                prescriptionTypeOptions:[],
                //可用的生产厂家
                producerOptions:[],
                // 分页数据总条数
                total: 0,
                pageSize:10,
                currentPage:1,
                // 弹出层标题
                title: '',
                // 是否显示弹出层
                open: false,
                medicNum:false,
                // 表单数据
                form: {},
                // 表单校验
                rules: {
                    medicinesName: [
                        { required: true, message: '药品名称不能为空', trigger: 'blur' }
                    ],
                    keywords: [
                        { required: true, message: '查询关键字不能为空', trigger: 'blur' }
                    ],
                    prescriptionPrice: [
                        { required: true, message: '处方价格不能为空', trigger: 'blur' }
                    ],
                    medicinesStockDangerNum: [
                        { required: true, message: '预警值不能为空', trigger: 'blur' }
                    ],
                    medicinesType: [
                        { required: true, message: '药品类型不能为空', trigger: 'blur' }
                    ],
                    prescriptionType: [
                        { required: true, message: '处方类型不能为空', trigger: 'blur' }
                    ],
                    producerId: [
                        { required: true, message: '生产厂家不能为空', trigger: 'blur' }
                    ],
                }
            }
        },
        created() {
            //加载状态
            this.initStatus()
            // 查询表格数据
            this.initMedicinesList()
        },
        methods:{
            //查询表格数据
            initMedicinesList(){
                this.$axios.post("/stock/medic/med/getAll/"+this.currentPage+"/"+this.pageSize,this.queryParams).then(result=>{
                    this.medicinesTableList=result.data.data.records;
                    this.total=result.data.data.total;
                })
            },
            // 分页pageSize变化时触发
            handleSizeChange(val) {
                this.pageSize = val
                // 重新查询
                this.initMedicinesList()
            },
            // 点击上一页  下一页，跳转到哪一页面时触发
            handleCurrentChange(val) {
                this.currentPage = val
                // 重新查询
                this.initMedicinesList()
                //查询厂家
            },
            //加载状态
            initStatus(){
                this.$axios.get("/stock/prod/dict/data/findByType/sys_normal_disable").then(result=>{
                    this.statusOptions=result.data.data;
                })
                // 查询药品处方分类字典数据
                this.$axios.get("/stock/prod/dict/data/findByType/his_prescription_type").then(result=>{
                    this.prescriptionTypeOptions=result.data.data;
                })
                // 查询药品分类字典数据
                this.$axios.get("/stock/prod/dict/data/findByType/his_medicines_type").then(result=>{
                    this.medicinesTypeOptions=result.data.data;
                })
                // 查询所有可用的生产厂家
                this.$axios.get("/stock/prod/prod/selectAllProdName/").then(res => {
                    this.producerOptions = res.data.data;
                })
            },
            // 数据表格的多选择框选择时触发
            handleSelectionChnage(selection) {
                this.ids = selection.map(item => item.medicinesId)
                this.single = selection.length !== 1
                this.multiple = !selection.length
                this.medicinesIds=selection
            },
            // 翻译药品类型
            medicinesTypeFormatter(row) {
                return this.formatDict(this.medicinesTypeOptions, row.medicinesType)
            },
            // 翻译处方类型
            prescriptionTypeFormatter(row) {
                return this.formatDict(this.prescriptionTypeOptions, row.prescriptionType)
            },
            // 翻译状态
            statusFormatter(row) {
                return this.formatDict(this.statusOptions, row.status)
            },
            // 翻译生产厂家
            producerFormatter(row) {
                let name = ''
                this.producerOptions.filter(item => {
                    if (parseInt(row.producerId) === parseInt(item.producerId)) {
                        name = item.producerName
                    }
                })
                return name
            },
            //搜索
            handleQuery(){
                this.currentPage=1,
                this.initMedicinesList()
            },
            //重置查询条件
            resetQuery(){
                this.resetForm('queryForm')
                this.initMedicinesList()
            },
            // 打开添加的弹出层
            handleAdd() {
                this.open = true
                this.reset()
                this.title = '添加生产药品信息'
            },
            // 打开修改的弹出层
            handleUpdate(row) {
                this.title = '修改生产药品信息'
                this.open = true
                this.reset()
                this.form=JSON.parse(JSON.stringify(row))
            },
            // 执行删除
            handleDelete(row) {
                const tx = this
                tx.$confirm('此操作将永久删除该生产药品数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    tx.$axios.delete("/stock/medic/med/deleteMedic/"+row.medicinesId).then(res => {
                        tx.msgSuccess('删除成功')
                        tx.currentPage=1
                        tx.initMedicinesList()// 全查询
                    }).catch(() => {
                        tx.msgError('删除失败')
                    })
                }).catch(() => {
                    tx.msgError('删除已取消')
                })
            },
            // 执行批量删除
            handleDeleteTop(row) {
                const tx = this
                tx.$confirm('此操作将永久删除该生产药品数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    tx.$axios.post("/stock/medic/med/delAll/",tx.medicinesIds).then(res => {
                        tx.msgSuccess('删除成功')
                        tx.currentPage=1
                        tx.initMedicinesList()// 全查询
                    }).catch(() => {
                        tx.msgError('删除失败')
                    })
                }).catch(() => {
                    tx.msgError('删除已取消')
                })
            },
            // 添加
            handleSubmit(form) {
                this.$refs['form'].validate((valid) => {
                    if (valid){
                        // 做添加
                        const tx = this
                        tx.$axios.post("/stock/medic/med/andMedic/",tx.form).then(res => {
                            if (tx.form.medicinesId === undefined) {
                                tx.msgSuccess('保存成功')
                                tx.initMedicinesList()// 列表重新查询
                                tx.open = false// 关闭弹出层
                            } else { // 做修改
                                tx.msgSuccess('修改成功')
                                tx.initMedicinesList()// 列表重新查询
                                tx.open = false// 关闭弹出层
                            }
                        })
                    }

                })
            },
            // 取消
            cancel() {
                this.open = false
                this.title = ''
            },
            //添加结束
            // 重置表单
            reset() {
                this.resetForm('form')
                this.form = {
                    medicinesId: undefined,
                    medicinesName: undefined,
                    medicinesNumber: undefined,
                    unit: undefined,
                    conversion: undefined,
                    prescriptionPrice: 0,
                    medicinesStockDangerNum: 0,
                    medicinesType: undefined,
                    prescriptionType: undefined,
                    keywords: undefined,
                    medicinesStockNum: 0,
                    status: '0'
                }
            },
            // 调整库存
            handleUpdateStorage(row) {
                const tx = this
                tx.$prompt('请输入要调整[' + row.medicinesName + ']的值', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    inputPattern: /^-?\d+$/,
                    inputErrorMessage: '库存值输入不正确'
                }).then(({ value }) => {
                    tx.form={
                        medicinesId: row.medicinesId,
                        medicinesStockNum:value
                    }
                    tx.$axios.post("/stock/medic/med/andMedic/",tx.form).then(res => {
                        tx.msgSuccess('调整成功')
                        tx.initMedicinesList()// 重新查询数据
                    }).catch(() => {
                        tx.msgError('调整失败')
                    })
                }).catch(() => {
                    tx.msgError('调整取消')
                })
            },
        }
    }
</script>

<style scoped>
    .demo-table-expand {
        font-size: 0;
    }
    .demo-table-expand label {
        width: 90px;
        color: #99a9bf;
    }
    .demo-table-expand .el-form-item {
        margin-right: 0;
        margin-bottom: 0;
        width: 50%;
    }
</style>
